﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="CanDoo.Oil.WebApp.financeBuyStatement.Edit" %>

<%@ Register Src="../UserControls/SelectSupplier.ascx" TagPrefix="uc1" TagName="SelectSupplier" %>
<%@ Register Src="../UserControls/SelectUser.ascx" TagPrefix="uc1" TagName="SelectUser" %>
<%@ Register Src="../UserControls/SelectBuyAdvanceMoney.ascx" TagPrefix="uc1" TagName="SelectBuyAdvanceMoney" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>采购结算单编辑</title>
    <link href="/res/css/common.css" rel="stylesheet" />
    <style>
        .f-grid-row-summary .f-grid-cell-inner {
            font-weight: bold;
            color: red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <f:PageManager ID="PageManager1" AutoSizePanelID="PanelMain" runat="server" />

        <f:Panel ID="PanelMain" ShowBorder="false" ShowHeader="false" AutoScroll="true" runat="server" Layout="VBox" BoxConfigAlign="Stretch" BoxConfigPosition="Start">
            <Toolbars>
                <f:Toolbar ID="Toolbar1" runat="server">
                    <Items>
                        <f:Button ID="btnClose" EnablePostBack="false" runat="server" Hidden="true"
                            Text="关闭">
                        </f:Button>
                        <f:Button ID="btnSave" ValidateForms="FormEdit" Hidden="true"
                            OnClick="btnSave_Click" runat="server" Text="保存">
                        </f:Button>
                        <f:Button ID="btnToEdit" Hidden="true"
                            OnClick="btnToEdit_Click" runat="server" Text="编辑信息">
                        </f:Button>
                        <f:HiddenField ID="ed_EntityId" runat="server"></f:HiddenField>
                    </Items>
                </f:Toolbar>
            </Toolbars>
            <Items>
                <f:Form BodyPadding="10px" ShowBorder="false" EnableCollapse="true"
                    ID="FormEdit" runat="server" ShowHeader="false" LabelAlign="Right">
                    <Items>
                        <f:Panel ID="Panel1" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:TextBox ID="ed_Code" runat="server" Label="结算单号" MaxLength="50" ColumnWidth="25%" Readonly="true" EmptyText="系统自动生成">
                                </f:TextBox>
                                <f:DropDownBox runat="server" ID="ed_Supplier" EmptyText="请选择供应商" DataControlID="SelectSupplierControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Required="true" ShowRedStar="True" Label="供应商" ColumnWidth="75%" AutoPostBack="true" OnTextChanged="ed_NeedPayDate_TextChanged">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectSupplier runat="server" ID="SelectSupplier" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel3" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:DropDownList ID="ed_PayType" runat="server" Label="付款方式" ColumnWidth="25%">
                                </f:DropDownList>
                                <f:DatePicker ID="ed_PayDate" runat="server" Required="true" ShowRedStar="True" Label="付款日期" ColumnWidth="25%">
                                </f:DatePicker>
                                <f:DropDownBox runat="server" ID="ed_User" EmptyText="请选择经办人" DataControlID="SelectUserControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Required="true" ShowRedStar="True" Label="经办人" ColumnWidth="50%">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectUser runat="server" ID="SelectUser" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel6" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:DropDownBox runat="server" ID="ed_BuyAdvanceMoney" EmptyText="请选择预付款" DataControlID="SelectBuyAdvanceMoneyControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Label="预付款" ColumnWidth="75%" AutoPostBack="true" OnTextChanged="ed_BuyAdvanceMoney_TextChanged" EnableClearIconClickEvent="true" OnClearIconClick="ed_BuyAdvanceMoney_TextChanged">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectBuyAdvanceMoney runat="server" ID="SelectBuyAdvanceMoney" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                                <f:NumberBox ID="ed_AdvanceMoney" runat="server" Required="true" ShowRedStar="True" Label="预付款金额" Readonly="true" ColumnWidth="25%">
                                </f:NumberBox>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel2" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:DatePicker ID="ed_NeedPayDate" runat="server" Label="到期日期" ColumnWidth="25%" AutoPostBack="true" OnTextChanged="ed_NeedPayDate_TextChanged">
                                </f:DatePicker>
                                <f:NumberBox ID="ed_ThisPayMoney" runat="server" Required="true" ShowRedStar="True" Label="本次支付" ColumnWidth="25%" Readonly="true">
                                </f:NumberBox>
                                <f:NumberBox ID="ed_ThisPayedLastMoney" runat="server" Required="true" ShowRedStar="True" Label="余额" ColumnWidth="25%" Readonly="true">
                                </f:NumberBox>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel8" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:TextArea ID="ed_Remark" runat="server" Label="备注" Height="80px" MaxLength="500">
                                </f:TextArea>
                            </Items>
                        </f:Panel>
                    </Items>
                </f:Form>
                <f:Grid ID="GridChild" ShowBorder="true" ShowHeader="true" Title="采购结算单明细" EnableCollapse="true" MinHeight="400px" BodyPadding="5px"
                    runat="server" DataKeyNames="Id" AllowCellEditing="true" EnableColumnLines="true" ShowSelectedCell="true" ClicksToEdit="1" BoxFlex="1" TabEditableCell="true" EnableSummary="true" SummaryPosition="Flow">
                    <Toolbars>
                        <f:Toolbar ID="Toolbar2" runat="server">
                            <Items>
<%--                                <f:Button ID="btnNew" Text="新增行" Icon="Add" EnablePostBack="false" runat="server">
                                </f:Button>--%>
                                <f:Button ID="btnDelete" Text="删除行" Icon="Delete" EnablePostBack="false" runat="server">
                                </f:Button>
                            </Items>
                        </f:Toolbar>
                    </Toolbars>
                    <Columns>
                        <f:TemplateField Width="40px">
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
                            </ItemTemplate>
                        </f:TemplateField> 
                        <f:RenderField Width="50px" ColumnID="BillId" DataField="BillId" FieldType="String" HeaderText="账单ID" Hidden="true">
                        </f:RenderField>
                        <f:RenderField Width="120px" ColumnID="Code" DataField="Code" FieldType="String" HeaderText="账单编号">
                        </f:RenderField> 
                        <f:RenderField ColumnID="Name" DataField="Name" FieldType="String" HeaderText="账单概要" ExpandUnusedSpace="true" MinWidth="100px">
                        </f:RenderField>
                        <f:RenderField Width="100px" ColumnID="CreatedDate" DataField="CreatedDate" FieldType="String" HeaderText="立账日期">
                        </f:RenderField>
                        <f:RenderField Width="100px" ColumnID="NeedDate" DataField="NeedDate" FieldType="String" HeaderText="应付日期">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="Money" DataField="Money" FieldType="double"
                            HeaderText="立账金额">
                        </f:RenderField>
                        <f:RenderField Width="70px" ColumnID="PayedMoney" DataField="PayedMoney" FieldType="double"
                            HeaderText="已冲账">
                        </f:RenderField>
                        <f:RenderField Width="70px" ColumnID="WaitMoney" DataField="WaitMoney" FieldType="double"
                            HeaderText="未冲账">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="thisPayMoney" DataField="thisPayMoney" FieldType="double"
                            HeaderText="本次结算">
                            <Editor>
                                <f:NumberBox ID="ed_child_thisPayMoney" runat="server" Required="true">
                                </f:NumberBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField Width="70px" ColumnID="thisPayedLastMonoey" DataField="thisPayedLastMonoey" FieldType="double"
                            HeaderText="余额">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="Remark" DataField="Remark" FieldType="String"
                            HeaderText="备注">
                            <Editor>
                                <f:TextBox ID="ed_child_Remark" runat="server">
                                </f:TextBox>
                            </Editor>
                        </f:RenderField>
                        <f:LinkButtonField ColumnID="Delete" HeaderText="删除" Width="80px" EnablePostBack="false" Text="删除" />
                    </Columns>
                    <Listeners>
                        <f:Listener Event="afteredit" Handler="onGridAfterEdit" />
                        <f:Listener Event="dataload" Handler="onGridDataLoad" />
                    </Listeners>
                </f:Grid>
            </Items>
        </f:Panel>
    </form>
    <script>
        var gridChildClientID = '<%= GridChild.ClientID %>';

        function renderDelete() {
            return '<a href="javascript:;" class="action-btn delete"><img class="f-grid-imagefield" src="/res/icon/delete.png"></a>';
        }

        function updateSummary() {
            var me = F(gridChildClientID), moneyTotal = 0, payedMoneyTotal = 0, waitMoneyTotal = 0, thisPayMoneyTotal = 0, thisPayedLastMonoeyTotal = 0;
            me.getRowEls().each(function (index, tr) {
                moneyTotal += me.getCellValue(tr, 'Money') * 1.0;
                payedMoneyTotal += me.getCellValue(tr, 'PayedMoney') * 1.0;
                waitMoneyTotal += me.getCellValue(tr, 'WaitMoney') * 1.0;
                thisPayMoneyTotal += me.getCellValue(tr, 'thisPayMoney') * 1.0;
                thisPayedLastMonoeyTotal += me.getCellValue(tr, 'thisPayedLastMonoey') * 1.0;
            });

            // 第三个参数 true，强制更新，不显示左上角的更改标识
            me.updateSummaryCellValue('Money', moneyTotal, true);
            me.updateSummaryCellValue('PayedMoney', payedMoneyTotal, true);
            me.updateSummaryCellValue('WaitMoney', waitMoneyTotal, true);
            me.updateSummaryCellValue('thisPayMoney', thisPayMoneyTotal, true);
            me.updateSummaryCellValue('thisPayedLastMonoey', thisPayedLastMonoeyTotal, true);

            F('<%= ed_ThisPayMoney.ClientID %>').setValue(thisPayMoneyTotal);
            F('<%= ed_ThisPayedLastMoney.ClientID %>').setValue(thisPayedLastMonoeyTotal);
        }

        function onGridAfterEdit(event, value, params) {
            if (params.columnId == "thisPayMoney") {
                var me = F(gridChildClientID);
                var p = params.rowData.values.thisPayMoney * 1.0;
                var n = params.rowData.values.WaitMoney * 1.0;
                var m = n - p;

                me.getRowEls().each(function (index, tr) {
                    if (me.getCellValue(tr, 'thisPayMoney') == p && me.getCellValue(tr, 'WaitMoney') == n) {
                        me.updateCellValue(tr, "thisPayedLastMonoey", m);
                    }
                });
                updateSummary();
            }
        }

        function onGridDataLoad() {
            updateSummary();
        }

        // 点击新增按钮
        //function onAddClick() {
        //    F(gridChildClientID).addNewRecord({}, true);
        //}

        // 点击删除按钮
        function onDeleteClick() {
            var gridChild = F(gridChildClientID);
            if (!gridChild.hasSelection()) {
                F.alert('请至少选择一项！');
                return false;
            }

            F.confirm({
                message: '删除选中行？',
                ok: function () {
                    gridChild.deleteSelectedRows();
                    // 删除行后，要记得更新合计行
                    updateSummary();
                }
            });
        }

        // 页面渲染完毕
        F.ready(function () {
            // 注册表格行内删除事件
            var gridChild = F(gridChildClientID);
            gridChild.el.on('click', '.action-btn.delete', function () {
                var targetEl = $(this);
                var rowEl = targetEl.parents('.f-grid-row');
                var rowData = gridChild.getRowData(rowEl);

                F.confirm({
                    message: '删除选中行？',
                    ok: function () {
                        gridChild.deleteRow(rowData.id);
                        // 删除行后，要记得更新合计行
                        updateSummary();
                    }
                });
            });
            //加载完成后显示合计行
            updateSummary();
        });
    </script>
</body>
</html>
